/**
 *    Copyright 2009-2020 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.builder;

import java.util.List;

import org.apache.ibatis.mapping.Discriminator;
import org.apache.ibatis.mapping.ResultMap;
import org.apache.ibatis.mapping.ResultMapping;

/**
 * ResultMap解析器
 * <resultMap id="detailedBlogResultMap" type="Blog">
 *   <constructor>
 *     <idArg column="blog_id" javaType="int"/>
 *   </constructor>
 *   <result property="title" column="blog_title"/>
 *   <association property="author" javaType="Author">
 *     <id property="id" column="author_id"/>
 *     <result property="username" column="author_username"/>
 *     <result property="password" column="author_password"/>
 *     <result property="email" column="author_email"/>
 *     <result property="bio" column="author_bio"/>
 *     <result property="favouriteSection" column="author_favourite_section"/>
 *   </association>
 *   <collection property="posts" ofType="Post">
 *     <id property="id" column="post_id"/>
 *     <result property="subject" column="post_subject"/>
 *     <association property="author" javaType="Author"/>
 *     <collection property="comments" ofType="Comment">
 *       <id property="id" column="comment_id"/>
 *     </collection>
 *     <collection property="tags" ofType="Tag" >
 *       <id property="id" column="tag_id"/>
 *     </collection>
 *     <discriminator javaType="int" column="draft">
 *       <case value="1" resultType="DraftPost"/>
 *     </discriminator>
 *   </collection>
 * </resultMap>
 * @author Eduardo Macarron
 */
public class ResultMapResolver {
  /**
   * 构建助手
   */
  private final MapperBuilderAssistant assistant;
  /**
   * resultMap的id
   */
  private final String id;
  /**
   * resultMap的类型
   */
  private final Class<?> type;
  /**
   * resultMap继承的resultMap 的id
   */
  private final String extend;
  /**
   * resultMap中的鉴别器
   */
  private final Discriminator discriminator;
  /**
   * resultMap中的节点信息
   */
  private final List<ResultMapping> resultMappings;
  /**
   * 是否自动映射
   */
  private final Boolean autoMapping;

  public ResultMapResolver(MapperBuilderAssistant assistant, String id, Class<?> type, String extend, Discriminator discriminator, List<ResultMapping> resultMappings, Boolean autoMapping) {
    this.assistant = assistant;
    this.id = id;
    this.type = type;
    this.extend = extend;
    this.discriminator = discriminator;
    this.resultMappings = resultMappings;
    this.autoMapping = autoMapping;
  }

  /**
   * 分析，这里主要是将这个resultMap信息，添加到构建助手的Map集合中
   * @return
   */
  public ResultMap resolve() {
    return assistant.addResultMap(this.id, this.type, this.extend, this.discriminator, this.resultMappings, this.autoMapping);
  }

}
